<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%
	Where where = new Where();
	Integer deptId = input.getInt("id", -1);
	//deptId = 1199;
	Map map = new HashMap();
	List<Map> depts = QBuilder.db("basedb").select("new map(deptName as title, branch as branch, (case when summary is NULL then '' else summary end) as summary, (case when img is NULL then '' else img end) as img, (case when content is NULL then '' else content end) as content, (case when imgs is NULL then '' else imgs end) as imgs)").from("Dept").where(where.and("hospital=", hospital).and("status = ", STATUS_ENABLE).and("id=", deptId)).query().getResultObj();
	String _branch = "-1";
	if (depts.size() > 0) {
		Map _map = depts.get(0);
		_branch = Tools.getDesc(_map.get("branch"), "-1");
		String content = Tools.getDesc(_map.get("content"));
		org.jsoup.nodes.Document d= Jsoup.parse(content);
		String summary = Tools.getDesc(_map.get("summary"), "");
		if ("".equals(summary)) {
			summary = d.body().text();
			if ("".equals(summary))
				summary = "暂无科室介绍";
			_map.put("summary", summary);
		}
	}
	map.put("dept", depts.size() == 0 ? new HashMap() : depts.get(0));   // 查询的科室信息

	where = new Where();
	if (!"-1".equals(_branch))
		where.and("dl.branch=", _branch);
	List<Map> expertIds = QBuilder.db("basedb").select("new map(s.doctorId as expert_id, s.doctorName as name)").from("dept_link as dl, Schedule as s").where(where.and("dl.hospital=", hospital).and("dl.status = ", STATUS_ENABLE).and("dl.dept_id=", deptId).and("dl.dept_type=", DEPT_EXPERT).and("s.registerType=2").and("s.hospital=", hospital).and("s.status = ", STATUS_ENABLE).and("dl.other_id = s.deptId")).group("s.doctorId").query().getResultObj();
	// 获取关联的专家

	if (isEmpty(expertIds)) {
		map.put("experts", new ArrayList());
	} else {
		String expertId = "'-1'";
		for (int i = 0; i < expertIds.size(); i++) {
			Map _map = expertIds.get(i);
			expertId += ",'" + Tools.getDesc(_map.get("expert_id"), "-1") + "'";
		}
		List<Map> experts = QBuilder.db("basedb").select("new map(e.id as id, e.name as name, (case when e.expertId is NULL then '' else e.expertId end) as expert_id, (case when e.avatar is NULL then '' else e.avatar end) as avatar, (case when e.title is NULL then '' else e.title end) as title)").from("Expert e").where(new Where("e.hospital=", hospital).and("e.status = ", STATUS_ENABLE).and("e.expertId in("+expertId+")")).query().getResultObj();
		List<Map> result = new ArrayList<Map>();
		for (int i = 0; i < expertIds.size(); i++) {
			Map _map = new HashMap();
			Map tempMap = expertIds.get(i);
			String doctorId = Tools.getDesc(tempMap.get("expert_id"), "-1");
			_map.put("expert_id", doctorId);
			_map.put("id", doctorId);
			_map.put("avatar", DEFAULT_AVATAR);
			_map.put("name", Tools.getDesc(tempMap.get("name"), ""));
			for (int j = 0; j < experts.size(); j++) {
				Map _tempMap = experts.get(j);
				String _doctorId = (String)_tempMap.get("expert_id");
				if (doctorId.equals(_doctorId)) {
					_map.put("id", _doctorId);
					_map.put("avatar", Tools.getDesc(_tempMap.get("avatar"), ""));
					_map.put("title", Tools.getDesc(_tempMap.get("title"), ""));
				}
			}
			result.add(_map);
		}
		map.put("experts", result);
	}
	// 查询科室的新闻信息
	String branch = "ks" + deptId; // 由ks + deptid组成
	String selectContent = "new map(n.id as id , n.newsId as news_id, n.url as url, n.title as title, n.summary as summary, n.time as time, n.img as img, n.jsonString as json, n.tag as tag, n.category as category)";
	List<Map> labels = QBuilder.db("basedb").select("new map(type as label_id, name as name, content as content, queue as queue, icon as icon, parent as parent, jsonString as json, branch as branch)").from("Label").where(new Where("hospital =" , hospital).and("status = ", STATUS_ENABLE).and("category=3")).query().getResultObj();
	for (int i = 0; i < labels.size(); i++) {
		Map _map = labels.get(i);
		Integer labelId = Tools.getInt(_map.get("label_id") + "", -1111);
		List<Map> newsList = QBuilder.db("basedb").select(selectContent).from("News n").where(new Where("n.hospital=", hospital).and("n.status = ", STATUS_ENABLE).and("n.branch=", branch).and("n.category=", labelId)).order("n.newsId", "desc").page(2,1).query().getResultObj();
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(NEWS_CATEGORY);
		oh.setOperate(LOVE);
		_map.put("news", love(query(newsList, oh), -1, oh));
	}
	map.put("news", labels);
	msg.put("data", map);
	Tools.encryptPrint(out, msg);
%>
